package com.yungam.imcs.agv.controller;

import com.yungam.common.response.RetDTO;
import com.yungam.imcs.agv.bean.AgvInfo;
import com.yungam.imcs.agv.bean.TaskInfo;
import com.yungam.imcs.agv.ws.WCSAPI;
import com.yungam.imcs.agv.ws.WCSAPISoap;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "agv 管理页面接口")
@RestController
@RequestMapping("/logistics")
public class AgvManageController {

    private final Boolean agv = false;

    @GetMapping("/avgInfo")
    public RetDTO<?> agvInfo() {
        List<AgvInfo> list = null;
        if (agv) {
            WCSAPISoap wcsapiSoap = new WCSAPI().getWCSAPISoap();
            String agvInfoResponse = wcsapiSoap.getAgvInfo();
            JSONObject jsonObject = XML.toJSONObject(agvInfoResponse);
            JSONObject arrayOfAgvInfo = jsonObject.getJSONObject("ArrayOfAgvInfo");
            JSONArray agvInfoList = arrayOfAgvInfo.getJSONArray("AgvInfo");
            list = new ArrayList<>();
            for (int i = 0; i < agvInfoList.length(); i++) {
                JSONObject agvInfoJson = agvInfoList.getJSONObject(i);
                AgvInfo agvInfo = new AgvInfo();
                agvInfo.setAgvId(agvInfoJson.getInt("AgvId"));
                agvInfo.setAgvName(agvInfoJson.getString("AgvName"));
                agvInfo.setChargeStatus(agvInfoJson.getInt("ChargeStatus"));
                agvInfo.setPosition(agvInfoJson.getInt("Position"));
                agvInfo.setWorkStatus(agvInfoJson.getInt("WorkStatus"));
                agvInfo.setBatteryPower(agvInfoJson.getInt("BatteryPower"));
                agvInfo.setRemarks(agvInfoJson.getString("Remarks"));
                list.add(agvInfo);
            }
        } else {
            list = new ArrayList<>();
            AgvInfo agvInfo1 = new AgvInfo();
            agvInfo1.setAgvId(1);
            agvInfo1.setAgvName("小车1");
            agvInfo1.setPosition(15);
            agvInfo1.setBatteryPower(95);
            agvInfo1.setChargeStatus(0);
            agvInfo1.setWorkStatus(0);

            AgvInfo agvInfo2 = new AgvInfo();
            agvInfo2.setAgvId(2);
            agvInfo2.setAgvName("小车2");
            agvInfo2.setPosition(31);
            agvInfo2.setBatteryPower(25);
            agvInfo2.setChargeStatus(1);
            agvInfo2.setWorkStatus(1);

            AgvInfo agvInfo3 = new AgvInfo();
            agvInfo3.setAgvId(3);
            agvInfo3.setAgvName("小车3");
            agvInfo3.setBatteryPower(50);
            agvInfo3.setChargeStatus(1);
            agvInfo3.setWorkStatus(0);
            agvInfo3.setPosition(17);

            list.add(agvInfo1);
            list.add(agvInfo2);
            list.add(agvInfo3);
        }
        return RetDTO.data(list);
    }

    @GetMapping("/taskInfo")
    public RetDTO<?> taskInfo() {
        List<TaskInfo> list = new ArrayList<>();
        if (agv) {
            WCSAPISoap wcsapiSoap = new WCSAPI().getWCSAPISoap();
            String taskInfoResponse = wcsapiSoap.getTaskInfo();
            JSONObject jsonObject = XML.toJSONObject(taskInfoResponse);
            System.out.println(jsonObject);

            JSONObject arrayOfAgvInfo = jsonObject.getJSONObject("ArrayOfTaskInfo");
            JSONArray taskInfoList = arrayOfAgvInfo.getJSONArray("TaskInfo");
            for (int i = 0; i < taskInfoList.length(); i++) {
                JSONObject taskInfoJson = taskInfoList.getJSONObject(i);
                TaskInfo taskInfo = new TaskInfo();
                taskInfo.setTaskId(taskInfoJson.getInt("TaskId"));
                taskInfo.setTaskName(taskInfoJson.getString("TaskName"));
                taskInfo.setAgvId(taskInfoJson.getInt("AgvId"));
                taskInfo.setAgvName(taskInfoJson.getString("AgvName"));
//            taskInfo.setRemarks(taskInfoJson.getString("Remarks"));
                if (StringUtils.isNotEmpty(taskInfoJson.getString("Route"))) {
                    taskInfo.setRoute(Arrays.stream(taskInfoJson.getString("Route").split(",")).map(Integer::valueOf).collect(Collectors.toList()));
                } else {
                    taskInfo.setRoute(new ArrayList<>());
                }
                list.add(taskInfo);
            }
        } else {
            TaskInfo taskInfo1 = new TaskInfo();
            taskInfo1.setTaskId(1);
            taskInfo1.setTaskName("任务1");
            taskInfo1.setRoute(Arrays.asList(5, 6, 12, 40, 14, 41, 15));
            taskInfo1.setAgvId(1);
            taskInfo1.setAgvName("小车1");

            TaskInfo taskInfo2 = new TaskInfo();
            taskInfo2.setTaskId(2);
            taskInfo2.setTaskName("任务2");
            taskInfo2.setRoute(Arrays.asList(12, 13, 42, 28, 43, 29, 30, 31, 32, 21, 20, 25));
            taskInfo2.setAgvId(2);
            taskInfo2.setAgvName("小车2");

            TaskInfo taskInfo3 = new TaskInfo();
            taskInfo3.setTaskId(3);
            taskInfo3.setTaskName("任务3");
            taskInfo3.setRoute(Arrays.asList(16, 17, 18, 19, 20, 25, 44));
            taskInfo3.setAgvId(3);
            taskInfo3.setAgvName("小车3");

            list.add(taskInfo1);
            list.add(taskInfo2);
            list.add(taskInfo3);
        }
        return RetDTO.data(list);
    }


//    @GetMapping("/updateRoute")
//    public void updateRoute(String route) {
//        routeList = Arrays.stream(route.split(",")).map(Integer::valueOf).collect(Collectors.toList());
//    }
//
//    @GetMapping("/updatePosition")
//    public void updatePosition(Integer position) {
//        POSITION = position;
//    }


}
